<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
        <el-form-item label="创建时间">
          <el-date-picker
                  v-model="dateRange"
                  size="small"
                  style="width: 240px"
                  value-format="yyyy-MM-dd"
                  type="daterange"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增Banner内容</el-button>
        </el-col>

      </el-row>
  
      <el-table ref="uiTable" v-loading="loading" :data="subjList" @selection-change="handleSelectionChange">
        <el-table-column type="index" label="序号" width="50"/>
        <el-table-column label="Banner描述" align="center" prop="majorName" >
            <template slot-scope="scope">
                <span v-if="scope.row.bannerTxt">
                    {{ scope.row.bannerTxt }}
                </span>
                <el-image v-else
                    style="width: 100px; height: 100px"
                    :src="url+scope.row.imagePath" >
                </el-image>
            </template>
        </el-table-column>

        <el-table-column label="Banner类型" align="center" prop="bannerType" >
            <template slot-scope="scope">
                <label v-if="scope.row.bannerType==2">文字</label>
                <label v-if="scope.row.bannerType==1">图片</label>
                <label v-if="scope.row.bannerType==3">电话</label>
                <label v-if="scope.row.bannerType==0">其他</label>
            </template>
        </el-table-column>
        <el-table-column label="排序" align="center" prop="sort" />
        <el-table-column label="创建时间" align="center" prop="createTime" >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="操作" align="center" >
          <template slot-scope="scope">
            <el-button
                    size="mini"
                    circle type="primary" icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    title="修改"
            />
            <el-button
                    size="mini"
                    circle type="danger" icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    title="删除"
            />
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="70%">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="Banner类型" prop="bannerType" >
            <el-radio v-model="form.bannerType" label="1" :disabled="form.id">图片</el-radio>
            <el-radio v-model="form.bannerType" label="2" :disabled="form.id">文字</el-radio>
            <el-radio v-model="form.bannerType" label="3" :disabled="form.id">电话</el-radio>
            <el-radio v-model="form.bannerType" label="0" :disabled="form.id">其他</el-radio>
          </el-form-item>
          <el-form-item label="排序" prop="sort" >
            <el-input-number v-model="form.sort" ></el-input-number>
          </el-form-item>
          <el-form-item label="Banner内容" v-if="form.bannerType==2 || form.bannerType==3 || form.bannerType==0">
            <el-input type="input"  v-model="form.bannerTxt" placeholder="请输入Banner内容" />
          </el-form-item>
          <el-form-item label="Banner图片" v-if="form.bannerType==1 || form.bannerType==0">
            <el-upload ref="upload"
              :class="{ disabled: uploadDisabled }"
              :action="updataUrl"
              list-type="picture-card"
              :on-success="handleSuccess"
              :on-remove="handleRemove"
              :multiple="false"
              :limit="1"
              :file-list="fileList"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
            
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import {getList, addOrUpdate, deleteById} from "@/api/workmgr/recruitBanner.js";
  
  export default {
    name: "recruitMajor",
    data() {
      return {
        
        url: process.env.VUE_APP_BASE_API + "/school/file/",
        dialogVisible:false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        indexs:[],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 科目表格数据
        subjList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态数据字典
        statusOptions: [],
        // 日期范围
        dateRange: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          type: undefined,
          status: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
            bannerType: [
                { required: true, message: "请选择Banner类型", trigger: "blur" }
            ]
        
        },
        fileList:[],
        updataUrl: process.env.VUE_APP_BASE_API + "/test/uploadImage",
      };
    },
    created() {
      this.getList();
      /*this.getDicts("t_sys_normal_disable").then(response => {
        this.statusOptions = response.data;
      });*/
    },
    computed: {
        uploadDisabled:function() {
            return this.fileList.length >0    
        },  
    },
    methods: {
        handleRemove(){
            this.fileList=[]
        },
      //上传失败
        handleError() {},
        //上传成功钩子
        handleSuccess(resp, file, filelist) {
            this.fileList = filelist
            if(resp.code==200){
                this.msgSuccess("图片上传成功");
                this.form.imagePath = resp.result
                console.log(this.form)
                return true
            }
        }, 
      /** 查询字典类型列表 */
      getList() {
        this.loading = true;
        getList(this.queryParams).then(response => {
            this.subjList = response.data.list;
            this.total = response.data.total;
            this.loading = false;
          }
        );
      },
      
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          recruitSynopsis: undefined,
          majorName: undefined,
          sort: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加科目";
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
  
        this.indexs = [];
        selection.forEach((val, index) => {
          this.subjList.forEach((v, i) => {
            // id 是每一行的数据id
            if(val.id == v.id){
              console.log(i+1);
              this.indexs.push(i+1);
            }
          })
        }) ;
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.form = row;
        this.fileList=[{name:"",url:this.url+row.imagePath}]
        this.open = true;
        
      },
      handleSocre(row) {
        this.$router.push({path:"/teacher/evaluation/mytask/taskScoreData", query:{teutId: row.teutId}})
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
                addOrUpdate(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.message);
                }
              });
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return deleteById(row);
          }).then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          }).catch(function() {});
      }
    }
  };
  </script>
  
  <style>
  .disabled .el-upload--picture-card {
    display: none;
  }

  .el-upload--picture-card .el-icon-plus {
    font-size: 28px;
    color: #8c939d;
    width: 150px;
    height: 150px;
    line-height: 150px;
    text-align: center;
  }

  </style>